package com.xin.ruanjian.omsystem.adapter;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;

import com.xin.ruanjian.omsystem.activity.MainActivity;
import com.xin.ruanjian.omsystem.fragment.CategoryFragment;
import com.xin.ruanjian.omsystem.fragment.MainFragment;
import com.xin.ruanjian.omsystem.fragment.OrderFragment;
import com.xin.ruanjian.omsystem.fragment.PersonFragment;
import com.xin.ruanjian.omsystem.utils.AnimationUtil;
import com.xin.ruanjian.omsystem.view.Fab;

/**
 * Created by local123 on 2018/2/2.
 */

public class MainVpAdapter extends FragmentPagerAdapter{
    private int mTabNums = 0;
    private MainFragment mainFragment;
    private CategoryFragment categoryFragment;
    private OrderFragment orderFragment;
    private PersonFragment personFragment;
    private Context context;
    private boolean isScrolled = false;

    public MainVpAdapter(Context context, FragmentManager fm, int mTabNums) {
        super(fm);
        this.mTabNums = mTabNums;
        this.context = context;
    }

    @Override
    public Fragment getItem(int position) {
        if (position == 0) {
            if (mainFragment == null) {
                mainFragment = new MainFragment();
                mainFragment.setOnScrollListener(new MainFragment.onScrollListener() {
                    @Override
                    public void upScroll() {
                        if (!isScrolled) {
                            ((MainActivity)context).fab.hide();
                            AnimationUtil.createOutAnimation(((MainActivity)context).ntbBottom.getMeasuredHeight(), ((MainActivity)context).rlBottom);
                            isScrolled = true;
                        }
                    }

                    @Override
                    public void downScroll() {
                        if (isScrolled) {
                            ((MainActivity)context).fab.show(0, 0);
                            AnimationUtil.createInAnimation(((MainActivity)context).ntbBottom.getMeasuredHeight(), ((MainActivity)context).rlBottom);
                            isScrolled = false;
                        }
                    }
                });
            }
            return mainFragment;
        }else if (position == 1) {
            if (categoryFragment == null) { categoryFragment = new CategoryFragment(); }
            return categoryFragment;
        }
//        else if (position == 2) {
//            if (orderFragment == null) { orderFragment = new OrderFragment(); }
//            return orderFragment;
//        }
        else {
            if (personFragment == null) { personFragment = new PersonFragment(); }
            return personFragment;
        }
    }

    @Override
    public int getCount() {
        return mTabNums;
    }
}
